namespace ghost{
    export class RoleBar extends GYSprite
    {
        public barBack:GYScaleSprite;
        public bar:GYScaleSprite;
        public barShadow:GYScaleSprite;
        public lab:Laya.Text;
        // public barShield:GYLite.GYScaleSprite;
        
        public barTween:GYTween;
        public barShadowTween:GYTween;
        // public barShieldTween:GYLite.GYTween;
        private _cur:number;
        private _curWidth:number;
        private _curShieldWidth:number;
        private _shield:number;
        private _max:number;
        private _barWidth:number;        
        // private _barShieldWidth:number;
        constructor()
        {
            super();
            let s = this;        
            //护盾
            // s.barShield = new GYLite.GYScaleSprite;
            // s.barShield.scale9GridRect=new GYLite.Scale9GridRect(30,30,2,2);
            // s.barShield.alpha = 1;
            // s.barShield.width = 0;
            // s.barShield.name = "barShield";
            // s.addChild(s.barShield);
            // s._barShieldWidth = 86;
            // s.barShield.x = 0;
            // s.barShield.y = 0;
            // s._curShieldWidth = 
            s._curWidth = NaN;

            s.barShadow = new GYScaleSprite;
            s.barShadow.name = "barShadow";
            s.barShadow.mode = ScaleMode.CLIP;
            s.addChild(s.barShadow);            
            s.bar = new GYScaleSprite();
            // s.bar.scale9GridRect = new Scale9GridRect(6,6,5,5);
            s.bar.mode = ScaleMode.CLIP
            s.bar.name = "bar";
            s.addChild(s.bar);                         
            s.lab = new Laya.Text;
            s.lab.align = "center";
            s.lab.color = "#ffffff";
            s.lab.stroke = 4;
            s.lab.strokeColor = "#000000";
            s.lab.fontSize = 20;            
            s.addChild(s.lab);
            s.setBarWidth();
        }
        public setBarWidth(barWidth: number=69,barOffsetX:number=0,barOffsetY:number=0): void {
            let s= this;
            s._barWidth = barWidth || 69;
            s.barShadow.x = s.bar.x = barOffsetX || 0;
            s.barShadow.y = s.bar.y = barOffsetY || 0;
            s.lab.width = s._barWidth;
        }
        public reset():void
        {
            let s= this;
            s._curShieldWidth = s._curWidth = NaN;            
        }
        public set barWidth(val:number)
        {               
            let s= this;
            s.bar.width = val;            
        }
        public get barWidth():number
        {
            return this.bar.width;
        }        
        
        // public set barShieldWidth(val:number)
        // {
        //     let s = this;
        //     s.barShield.width = val;           
        // }
        // public get barShieldWidth():number
        // {
        //     return this.barShield.width;
        // }
        public set barShadowWidth(val:number)
        {
            this.barShadow.width = val;            
        }
        public get barShadowWidth():number
        {
            return this.barShadow.width;
        }
        public setBarSkin(skin:string, atlas:string):void
        {
            this.bar.bitmapData = Main.instance.getRes(Conf.image + atlas + skin);
        }
        public setBarBackSkin(skin:string, atlas:string):void
        {
            let s= this;
            if(skin)
            {
                if(s.barBack == null)
                {
                    s.barBack = new GYScaleSprite;
                    s.addChildAt(s.barBack, 0);
                }
            }
            if(s.barBack)
                s.barBack.bitmapData = Main.instance.getRes(Conf.image + atlas + skin);
        }
        public setBarShadowSkin(skin:string, atlas:string):void
        {
            this.barShadow.bitmapData = skin?Main.instance.getRes(Conf.image + atlas + skin):null;
            this.barShadow.visible = !!skin;
        }
        // public setBarShieldSkin(skin:string, atlas:string):void
        // {
        //     this.barShield.bitmapData = Main.instance.getRes(atlas + skin);
        //     this.barShield.visible = !!skin;
        // }        
        public setBarValue(cur:number, max:number, shield:number=0,tween:boolean=true):void
        {
            let s= this;
            let per:number,val:number,dis:number,perShield:number,valShield:number;
            s._cur = cur;
            s._max = max;
            s._shield = shield;
            per = cur / (max + s._shield);
            perShield = (cur + s._shield) / (max + s._shield);
            val = Math.floor(per*s._barWidth);
            valShield = perShield*s._barWidth;
            s.lab.text = `${cur}/${max}`;
            if(val != s._curWidth)
            {
                dis = val - s._curWidth;
                if(dis < 0)dis = -dis;
                s._curWidth = val;
                if(s.barTween)
                {
                    s.barTween.clear();            
                    s.barTween = null;
                }                    
                if(tween && dis > 10)//优化成像素幅度大于10才进行tween
                {
                    s.barTween = GYTween.to(s,[TweenData.getInstance("barWidth",val)],100,0,null,null,null,null,true, false);
                    if(s.barShadow.visible)
                    {
                        if(s.barShadowTween)
                            s.barShadowTween.clear();
                        s.barShadowTween = GYTween.to(s,[TweenData.getInstance("barShadowWidth",valShield)],200,100,null,null,null,null,true, false);
                    }
                    
                }
                else
                {
                    s.barWidth = val;
                    s.barShadowWidth = valShield;
                }
            }            
            
            // if(s.barShield.visible)
            // {
            //     per = (s._shield + cur) / (max + s._shield);
            //     val = per*s._barWidth;
            //     if(val != s._curShieldWidth)
            //     {
            //         s._curShieldWidth = val;
            //         s.barShieldWidth = val;
            //     }            
            // }            
        }
        public destroy(destroyChild?: boolean):void
        {
            let s = this;            
            s.clearTween();            
            super.destroy(destroyChild);
        }
        public clearTween():void
        {
            let s = this;
            if(s.barTween)
            {
                s.barTween.clear();
                s.barTween = null;
            }
            if(s.barShadowTween)
            {
                s.barShadowTween.clear();
                s.barShadowTween = null;
            }
            // if(s.barShieldTween)
            // {
            //     s.barShieldTween.clear();
            //     s.barShieldTween = null;
            // }
        }
        
    }
}